package graph;

import java.io.IOException;
import java.util.StringTokenizer;

import model.Edge;
import model.GraphNew;
import model.Vertex;

/**
 * 
 * @author rocha
 */
public class GraphFactory {
	
	/**
	 * Create graph structure by input file referenced
	 * @param f file to contain the graph
	 * @return graph structure
	 * @throws IOException
	 */
	public static Graph createGraph(java.io.File f) throws IOException {
		
		/*
		 * load file
		 */
		java.io.BufferedReader reader = 
			new java.io.BufferedReader( new java.io.FileReader( f ) );
		
		/*
		 * create principals variables
		 */
		final int numberOfVertex = Integer.parseInt( reader.readLine() );
		final Graph graph = new Graph( numberOfVertex );
		
		return importGraph(reader, graph, numberOfVertex);		
	}
	
	/**
	 * Populate graph with the information in file that represent structure 
	 * @param graph graph
	 * @param numberOfVertex number of vertex
	 * return Graph object populated
	 */
	@SuppressWarnings("unchecked")
	private static Graph importGraph(final java.io.BufferedReader reader, final Graph graph, final int numberOfVertex) throws IOException {
		
		/*
		 * create principals variables
		 */
		int info;
		
		/*
		 * iterate line obtainer adjacents of the vertex
		 */
		for (int v = 0; v < numberOfVertex; v ++) {
			graph.getGraph()[v] = new java.util.ArrayList();
			
			StringTokenizer tok = new StringTokenizer( reader.readLine() );
			
			while (tok.hasMoreTokens()) {
				info = Integer.parseInt( tok.nextToken().trim() );				
				if (info != -1)
					graph.getGraph()[v].add( info );
			}
		}
		
		return graph;
	}	
	
	

}